package com.exercise.v2;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class Exercise {
    private List<AbstractExpression> expressions;
    private String title;
    private Random random;

    public Exercise() {
        this.expressions = new ArrayList<>();
        this.title = "数学练习";
        this.random = new Random();
    }
    public Exercise(String title) {
        this.expressions = new ArrayList<>();
        this.title = title;
        this.random = new Random();
    }
    public void addExpression(AbstractExpression expression) {
        expressions.add(expression);
    }
    public List<AbstractExpression> getExpressions() {
        return expressions;
    }
    public int getSize() {
        return expressions.size();
    }
    public String getTitle() {
        return title;
    }
    public void setTitle(String title) {
        this.title = title;
    }
    public AbstractExpression getExpression(int index) {
        if (index >= 0 && index < expressions.size()) {
            return expressions.get(index);
        }
        return null;
    }
    public void clear() {
        expressions.clear();
    }
    public void generateAdditionExercises(int count, int minValue, int maxValue) {
        for (int i = 0; i < count; i++) {
            int operand1 = random.nextInt(maxValue - minValue + 1) + minValue;
            int operand2 = random.nextInt(maxValue - minValue + 1) + minValue;
            expressions.add(new AdditionExpression(operand1, operand2));
        }
    }
    public void generateSubtractionExercises(int count, int minValue, int maxValue) {
        for (int i = 0; i < count; i++) {
            int operand1 = random.nextInt(maxValue - minValue + 1) + minValue;
            int operand2 = random.nextInt(operand1 + 1); // 确保结果非负
            expressions.add(new SubtractionExpression(operand1, operand2));
        }
    }
    public void generateMixedExercises(int count, int minValue, int maxValue) {
        for (int i = 0; i < count; i++) {
            if (random.nextBoolean()) {
                // 生成加法
                int operand1 = random.nextInt(maxValue - minValue + 1) + minValue;
                int operand2 = random.nextInt(maxValue - minValue + 1) + minValue;
                expressions.add(new AdditionExpression(operand1, operand2));
            } else {
                // 生成减法
                int operand1 = random.nextInt(maxValue - minValue + 1) + minValue;
                int operand2 = random.nextInt(operand1 + 1);
                expressions.add(new SubtractionExpression(operand1, operand2));
            }
        }
    }
    public String formatDisplay() {
        StringBuilder sb = new StringBuilder();
        sb.append("========== ").append(title).append(" ==========\n");
        for (int i = 0; i < expressions.size(); i++) {
            sb.append(String.format("%2d. %s\n", i + 1, expressions.get(i).toString()));
        }
        sb.append("==========================================\n");
        return sb.toString();
    }
    public String formatDisplayQuestions() {
        StringBuilder sb = new StringBuilder();
        sb.append("========== ").append(title).append(" ==========\n");
        for (int i = 0; i < expressions.size(); i++) {
            sb.append(String.format("%2d. %s\n", i + 1, expressions.get(i).toQuestionString()));
        }
        sb.append("==========================================\n");
        return sb.toString();
    }
}

